package com.fenghen.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.fenghen.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.fenghen.system.domain.RiskIdentification;
import com.fenghen.system.mapper.RiskMonitoringMapper;
import com.fenghen.system.domain.RiskMonitoring;
import com.fenghen.system.service.IRiskMonitoringService;
import com.fenghen.common.core.text.Convert;

/**
 * 风险监控与预警模块Service业务层处理
 * 
 * @author fenghen
 * @date 2024-11-06
 */
@Service
public class RiskMonitoringServiceImpl implements IRiskMonitoringService 
{
    @Autowired
    private RiskMonitoringMapper riskMonitoringMapper;

    /**
     * 查询风险监控与预警模块
     * 
     * @param monitorId 风险监控与预警模块主键
     * @return 风险监控与预警模块
     */
    @Override
    public RiskMonitoring selectRiskMonitoringByMonitorId(Long monitorId)
    {
        return riskMonitoringMapper.selectRiskMonitoringByMonitorId(monitorId);
    }

    /**
     * 查询风险监控与预警模块列表
     * 
     * @param riskMonitoring 风险监控与预警模块
     * @return 风险监控与预警模块
     */
    @Override
    public List<RiskMonitoring> selectRiskMonitoringList(RiskMonitoring riskMonitoring)
    {
        return riskMonitoringMapper.selectRiskMonitoringList(riskMonitoring);
    }

    /**
     * 新增风险监控与预警模块
     * 
     * @param riskMonitoring 风险监控与预警模块
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRiskMonitoring(RiskMonitoring riskMonitoring)
    {
        int rows = riskMonitoringMapper.insertRiskMonitoring(riskMonitoring);
        insertRiskIdentification(riskMonitoring);
        return rows;
    }

    /**
     * 修改风险监控与预警模块
     * 
     * @param riskMonitoring 风险监控与预警模块
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRiskMonitoring(RiskMonitoring riskMonitoring)
    {
        riskMonitoringMapper.deleteRiskIdentificationByRiskId(riskMonitoring.getMonitorId());
        insertRiskIdentification(riskMonitoring);
        return riskMonitoringMapper.updateRiskMonitoring(riskMonitoring);
    }

    /**
     * 批量删除风险监控与预警模块
     * 
     * @param monitorIds 需要删除的风险监控与预警模块主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskMonitoringByMonitorIds(String monitorIds)
    {
        riskMonitoringMapper.deleteRiskIdentificationByRiskIds(Convert.toStrArray(monitorIds));
        return riskMonitoringMapper.deleteRiskMonitoringByMonitorIds(Convert.toStrArray(monitorIds));
    }

    /**
     * 删除风险监控与预警模块信息
     * 
     * @param monitorId 风险监控与预警模块主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskMonitoringByMonitorId(Long monitorId)
    {
        riskMonitoringMapper.deleteRiskIdentificationByRiskId(monitorId);
        return riskMonitoringMapper.deleteRiskMonitoringByMonitorId(monitorId);
    }

    /**
     * 新增风险识别模块信息
     * 
     * @param riskMonitoring 风险监控与预警模块对象
     */
    public void insertRiskIdentification(RiskMonitoring riskMonitoring)
    {
        List<RiskIdentification> riskIdentificationList = riskMonitoring.getRiskIdentificationList();
        Long monitorId = riskMonitoring.getMonitorId();
        if (StringUtils.isNotNull(riskIdentificationList))
        {
            List<RiskIdentification> list = new ArrayList<RiskIdentification>();
            for (RiskIdentification riskIdentification : riskIdentificationList)
            {
                riskIdentification.setRiskId(monitorId);
                list.add(riskIdentification);
            }
            if (list.size() > 0)
            {
                riskMonitoringMapper.batchRiskIdentification(list);
            }
        }
    }
}
